package com.ztesoft.sca.controller;

import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.user.ImOrganizationPo;
import com.ztesoft.sca.model.user.ImRolePo;
import com.ztesoft.sca.model.user.ImUserPo;
import com.ztesoft.sca.service.ImOrgService;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;
import com.ztesoft.sca.util.seq.SequenceCreator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/*
 * hxz 组织操作类
 * */

@RestController
public class ImOrgController {
	private static final Logger logger = Logger.getLogger(ImOrgController.class);
    
	@Autowired
    private ImOrgService ioService;
	
    /*
     * hxz 组织查询
     * */
	@RequestMapping(value = "/imorg/list",method = {RequestMethod.GET})
	public String imOrgList(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imorg--list-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		int from=qryJson.getInt("page");//第1页开始
		int size=qryJson.getInt("rownum"); //每页大小
		
		Long orgId=qryJson.getLong("orgId");
		Long areaId=qryJson.getLong("areaId");
		String orgCode=qryJson.getString("orgCode");
		String orgName=qryJson.getString("orgName");
		Long parentId=qryJson.getLong("parentId");
//		String orgCodePath=qryJson.getString("orgCodePath");
//		String orgNamePath=qryJson.getString("orgNamePath");
		String orgAlias=qryJson.getString("orgAlias");
		
		ImOrganizationPo ioDto=new ImOrganizationPo();
		ioDto.setOrgId(orgId);
		ioDto.setAreaId(areaId);
		ioDto.setOrgCode(orgCode);
		ioDto.setOrgName(orgName);
		ioDto.setParentId(parentId);
		ioDto.setOrgAlias(orgAlias);
		
		List<ImOrganizationPo> list = new ArrayList<>();
		try {
			list = this.ioService.listImOrg(ioDto, from, size);
		} catch (Exception e) {
			 logger.error(" ImOrgList occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(ImOrganizationPo pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("orgId", pLog.getOrgId());
            jo.put("areaId", pLog.getAreaId());
            jo.put("orgCode", pLog.getOrgCode());
            jo.put("orgName", pLog.getOrgName());
            jo.put("parentId", pLog.getParentId());
            jo.put("orgCodePath", pLog.getOrgCodePath());
            jo.put("orgNamePath", pLog.getOrgNamePath());
            jo.put("orgAlias", pLog.getOrgAlias());
            
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", from);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
		
	
	 /*
     * hxz 查询组织下角色
     * */
	@RequestMapping(value = "/imorg/listRole",method = {RequestMethod.GET})
	public String imOrgListRole(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imorg--listrole-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		int from=qryJson.getInt("page");//第1页开始
		int size=qryJson.getInt("rownum"); //每页大小
		
		Long orgId=qryJson.getLong("orgId");
		Long areaId=qryJson.getLong("areaId");
		String orgCode=qryJson.getString("orgCode");
		String orgName=qryJson.getString("orgName");
		Long parentId=qryJson.getLong("parentId");
//		String orgCodePath=qryJson.getString("orgCodePath");
//		String orgNamePath=qryJson.getString("orgNamePath");
		String orgAlias=qryJson.getString("orgAlias");
		
		ImOrganizationPo ioDto=new ImOrganizationPo();
		ioDto.setOrgId(orgId);
		ioDto.setAreaId(areaId);
		ioDto.setOrgCode(orgCode);
		ioDto.setOrgName(orgName);
		ioDto.setParentId(parentId);
		ioDto.setOrgAlias(orgAlias);
		
		List<ImRolePo> list = new ArrayList<>();
		try {
			list = this.ioService.listImOrgRole(ioDto, from, size);
		} catch (Exception e) {
			 logger.error(" ImOrgListRole occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(ImRolePo pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("roleId", pLog.getRoleId());
            jo.put("roleCode", pLog.getRoleCode());
            jo.put("roleName", pLog.getRoleName());
            jo.put("comments", pLog.getComments());
            
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", from);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
	
	
	 /*
     * hxz 查询组织下人员
     * */
	@RequestMapping(value = "/imorg/listUser",method = {RequestMethod.GET})
	public String imOrgListUser(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imorg--listUser-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		int from=qryJson.getInt("page");//第1页开始
		int size=qryJson.getInt("rownum"); //每页大小
		
		Long orgId=qryJson.getLong("orgId");
		Long areaId=qryJson.getLong("areaId");
		String orgCode=qryJson.getString("orgCode");
		String orgName=qryJson.getString("orgName");
		Long parentId=qryJson.getLong("parentId");
//		String orgCodePath=qryJson.getString("orgCodePath");
//		String orgNamePath=qryJson.getString("orgNamePath");
		String orgAlias=qryJson.getString("orgAlias");
		
		ImOrganizationPo ioDto=new ImOrganizationPo();
		ioDto.setOrgId(orgId);
		ioDto.setAreaId(areaId);
		ioDto.setOrgCode(orgCode);
		ioDto.setOrgName(orgName);
		ioDto.setParentId(parentId);
		ioDto.setOrgAlias(orgAlias);
		
		List<ImUserPo> list = new ArrayList<>();
		try {
			list = this.ioService.listImOrgUser(ioDto, from, size);
		} catch (Exception e) {
			 logger.error(" ImOrgListUser occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(ImUserPo pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("userId", pLog.getUserId());
            jo.put("passwd", pLog.getPasswd());
            jo.put("userName", pLog.getUserName());
            jo.put("nickName", pLog.getNickName());
            jo.put("sign", pLog.getSign());
            jo.put("avatar", pLog.getAvatar());
            jo.put("mobileTel", pLog.getMobileTel());
            jo.put("homeTel", pLog.getHomeTel());
            jo.put("email", pLog.getEmail());
            jo.put("registerTime", pLog.getRegisterTime());
            
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", from);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
	
	/*
	 * hxz 组织新增
	 * */
	@RequestMapping(value = "/imorg/add",method = {RequestMethod.POST})
	public String imOrgAdd(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imorg--add-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        long orgId = 0L;
        try {
        	orgId = SequenceCreator.getSequence("IM_ORGANIZATION_SEQ");
        } catch (Exception e) {
           logger.error("sequenceCreator obtain IM_ORGANIZATION occur an error!",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
        }
        
		Long areaId=json.getLong("areaId");
		String orgCode=json.getString("orgCode");
		String orgName=json.getString("orgName");
		Long parentId=json.getLong("parentId");
		String orgCodePath=json.getString("orgCodePath");
		String orgNamePath=json.getString("orgNamePath");
		String orgAlias=json.getString("orgAlias");
		
		ImOrganizationPo ioDto=new ImOrganizationPo();
		ioDto.setOrgId(orgId);
		ioDto.setAreaId(areaId);
		ioDto.setOrgCode(orgCode);
		ioDto.setOrgName(orgName);
		ioDto.setParentId(parentId);
		ioDto.setOrgCodePath(orgCodePath);
		ioDto.setOrgNamePath(orgNamePath);
		ioDto.setOrgAlias(orgAlias);
       
       try {
           this.ioService.insertImOrg(ioDto);
       } catch (Exception e) {
           logger.error("create IM_ORGANIZATION occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,ioDto);
	}
		
	/*
	 * hxz 组织修改
	 * */
	@RequestMapping(value = "/imorg/modify",method = {RequestMethod.POST})
	public String imOrgModify(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imorg--modify-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        
        Long orgId=json.getLong("orgId");
//      Long areaId=json.getLong("areaId");
		String orgCode=json.getString("orgCode");
		String orgName=json.getString("orgName");
//		Long parentId=json.getLong("parentId");
		String orgCodePath=json.getString("orgCodePath");
		String orgNamePath=json.getString("orgNamePath");
		String orgAlias=json.getString("orgAlias");
		
		ImOrganizationPo ioDto=new ImOrganizationPo();
		ioDto.setOrgId(orgId);
//		ioDto.setAreaId(areaId);
		ioDto.setOrgCode(orgCode);
		ioDto.setOrgName(orgName);
//		ioDto.setParentId(parentId);
		ioDto.setOrgCodePath(orgCodePath);
		ioDto.setOrgNamePath(orgNamePath);
		ioDto.setOrgAlias(orgAlias);
       
       try {
           this.ioService.modifyImOrg(ioDto);
       } catch (Exception e) {
           logger.error("modify IM_ORGANIZATION occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,ioDto);
	}
	
	/*
	 * hxz 组织删除
	 * */
	@RequestMapping(value = "/imorg/del",method = {RequestMethod.POST})
	public String imOrgDel(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imorg--del-----");
        JSONObject json = JSONObject.fromObject(requestBody);
             
        Long orgId=json.getLong("orgId");
        
        ImOrganizationPo ioDto=new ImOrganizationPo();
        ioDto.setAreaId(orgId);        
       
       try {
           this.ioService.delImOrg(ioDto);
       } catch (Exception e) {
           logger.error("del IM_ORGANIZATION occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,ioDto);
	}
}
